#!/bin/bash
# author:zgy
# 2023.4.7

# 脚本命令执行报错退出显示
set -e

# 获取输入参数
parameter=$1

#################版本对应脚本参数、init参数、start参数######################
# master 版本
master_parameter_init_start=master

# OpenHarmony-3.2-beta2 版本
OpenHarmony_3_2_beta2_parameter=OpenHarmony-3.2-beta2
OpenHarmony_3_2_beta2_init_start=OpenHarmony-3.2-Beta2

# OpenHarmony-3.2-beta2-Merge 版本
OpenHarmony_3_2_beta2_Merge_parameter_init=OpenHarmony-3.2-beta2-Merge
OpenHarmony_3_2_beta2_Merge_start=OpenHarmony-3.2-beta2

# OpenHarmony-3.2-Release 版本
OpenHarmony_3_2_Release_parameter_init_start=OpenHarmony-3.2-Release
#########################################################################

# 下载代码相应参数
parameter_url=https://gitee.com/openharmony/manifest.git
parameter_init=""
parameter_xml=""
parameter_start=""

# 显示菜单
function show_menu() {
  echo -e "\e[36m*******************************************\e[0m"
  echo -e "\e[36m* Welcome to download OpenHarmony Code    *\e[0m"
  echo -e "\e[36m* Please Choice OpenHarmony SDK:          *\e[0m"
  echo -e "\e[36m* OpenHarmony Version                     *\e[0m"
  echo -e "\e[36m*   OpenHarmony master          prese 1.1 *\e[0m"
  echo -e "\e[36m*   OpenHarmony-3.2-beta2       press 1.2 *\e[0m"
  echo -e "\e[36m*   OpenHarmony-3.2-beta2-Merge press 1.3 *\e[0m"
  echo -e "\e[36m*   OpenHarmony-3.2-Release     press 1.4 *\e[0m"
  echo -e "\e[36m*******************************************\e[0m"
  echo -e "mif you don't want to download, you can input [\e[32mno\e[0m] to exit!"
  echo -e "mplease input version number(\e[32mexample 1.1\e[0m): "

  read -r version_number
  parameter=${version_number}
  check_parameter
}

# 显示下载信息
function show_download_information() {
  echo -e "\e[32m=================================================================================\e[0m"
  echo -e "\e[32myou have been download OpenHarmony ${parameter} code\e[0m"
  echo -e "\e[32murl       : ${parameter_url}\e[0m"
  echo -e "\e[32mbranch    : ${parameter}\e[0m"
  echo -e "\e[32mxml_name  : ${parameter_xml}\e[0m"
  echo -e "\e[32mcode_path : $(pwd)\e[0m"
  echo -e "\e[32minit   cmd: repo init -u ${parameter_url} -b ${parameter_init} -m ${parameter_xml} --no-repo-verify\e[0m"
  echo -e "\e[32msync   cmd: repo sync -c\e[0m"
  echo -e "\e[32mlfs    cmd: repo forall -c 'git lfs pull'\e[0m"
  echo -e "\e[32mset_br cmd: repo start ${parameter_start} --all\e[0m"
  echo -e "\e[32m=================================================================================\e[0m"
}

# 创建并进入版本目录
function make_cd_directory() {
  if [ ! -e ${parameter} ]; then
    mkdir ${parameter}
    cd ${parameter}
  elif [ ! -e ${parameter}_temp ]; then
    mkdir ${parameter}_temp
    cd ${parameter}_temp
  else
    rm -rf ${parameter}_temp
    mkdir ${parameter}_temp
    cd ${parameter}_temp
  fi
}

# 下载代码
function download() {
  #repo init -u ${parameter_url} -b ${parameter_init} -m ${parameter_xml} --no-repo-verify
  #repo sync -c
  #repo forall -c 'git lfs pull'
  #repo start ${parameter_start} --all
  echo -e "."
  echo -e "."
  echo -e "."
  echo -e "\e[32mdownload successfully !\e[0m"
}

# 查询参数
function check_parameter() {
  if [ "${parameter}" == "1.1" ]; then
    parameter=${master_parameter_init_start}
    parameter_init=${master_parameter_init_start}
    parameter_xml="default.xml"
    parameter_start=${master_parameter_init_start}
  elif [ "${parameter}" == "1.2" ]; then
    parameter=${OpenHarmony_3_2_beta2_parameter}
    parameter_init=${OpenHarmony_3_2_beta2_init_start}
    parameter_xml="default.xml"
    parameter_start=${OpenHarmony_3_2_beta2_init_start}
  elif [ "${parameter}" == "1.3" ]; then
    parameter=${OpenHarmony_3_2_beta2_Merge_parameter_init}
    parameter_init=${OpenHarmony_3_2_beta2_Merge_parameter_init}
    parameter_xml="thead_develop.xml"
    parameter_start=${OpenHarmony_3_2_beta2_Merge_start}
  elif [ "${parameter}" == "1.4" ]; then
    parameter=${OpenHarmony_3_2_Release_parameter_init_start}
    parameter_init=${OpenHarmony_3_2_Release_parameter_init_start}
    parameter_xml="default.xml"
    parameter_start=${OpenHarmony_3_2_Release_parameter_init_start}
  elif [ "${parameter}" == "no" ]; then
    return 0
  else
    clear
    echo -e "\e[31mproduct is not in product list, please recheck \e[0m"
    show_menu
    return 0
  fi
  make_cd_directory
  download
  show_download_information
}

function main() {
  clear
  if [ ! "${parameter}" ]; then
    show_menu
  else
    check_parameter
  fi
}

main
